#include "define.h"
#include "disp.h"
#include "power.h"
#include "clk.h"
#include "gpio.h"
#include "time.h"
#include "uart.h"
#include "spk.h"
#include "flash.h"
#include "adc.h"
#include "algo.h"
#include "bp.h"
#include "func.h"
#include "op.h"
#include "pwm.h"
#include "voice.h"
#include "queue.h"
#include "flash.h"
#include "uart_flash.h"
#include "w25qxx.h"
#include "transfer_BIN_to_EX_FLASH.h"




void uart_to_flash_init(void)
{
    RCC_SYSCLKConfig(RCC_SYSCLK_IHRC, RCC_SYSCLK_DIV1); // sys选择内部高速24MHz时钟
	TM_ITCmd(TM0,DISABLE);
    W25QXX_Init();

    UART_DeInit(UART1);
    RCC_APBPeriphClockCmd(RCC_APBPeriph_UART1, ENABLE);                    //外设时钟使能
    RCC_APB2PeriphClockConfig(RCC_APBPeriph_UART1,RCC_UARTCLK_SYSCLK,0);

    UART_InitTypeDef  UART_InitStruct;
    UART_InitStruct.UART_BaudRate=9600;
    UART_InitStruct.UART_Mode=UART_Mode_TRx;
    UART_InitStruct.UART_Parity=UART_Parity_None;
    UART_InitStruct.UART_StopBits=UART_StopBits_1;
    UART_InitStruct.UART_WordLength=UART_WordLength_8b;
    UART_InitStruct.UART_CarryWave=UART_CarryWave_None;

    UART_Init(UART1,&UART_InitStruct);

    UART_ClearFlagStatus(UART1, UART_Flag_RXNE);
    UART_ITConfig(UART1, UART_IT_RXNEIE, ENABLE);
    UART_ITConfig(UART1, UART_IT_TXEIE, DISABLE);

    UART_Cmd(UART1,ENABLE);

    VIC_InitTypeDef  VIC_InitStruct;
    RCC_APBPeriphClockCmd(RCC_APBPeriph_UART1, ENABLE);      //使能外部中断和按键中断模块的时钟

    VIC_InitStruct.VIC_IRQChannel=UART1_IRQn;
    VIC_InitStruct.VIC_IRQChannelPriority=VIC_IRQChannelPriority0;
    VIC_InitStruct.VIC_PrioThreshold=VIC_PrioThreshold0;
    VIC_InitStruct.VIC_PrioThresholdCmd=ENABLE;
    VIC_InitStruct.VIC_IRQChannelWakeUpLowPowerCmd=ENABLE;
    VIC_InitStruct.VIC_IRQChannelCmd=ENABLE;

    VIC_Init(&VIC_InitStruct);
#if 22
    UART_UART1_IOMAP(UART1_IOMAP_P36_P37); //将串口1复用
	GPIO_InitTypeDef  GPIO_InitStruct;

	GPIO_InitStruct.GPIO_Pin 		=	GPIO_Pin_7 ; //RST、DATA
	GPIO_InitStruct.GPIO_Mode 		=	GPIO_Mode_OUT;//GPIO_Mode_IN
	GPIO_InitStruct.GPIO_PuPd 		=	GPIO_PuPd_NOPULL;//选择开启上拉 GPIO_PuPd_NOPULL;
	GPIO_InitStruct.GPIO_PTSR	 	=	GPIO_PTSR_STRONG;
	GPIO_Init(GPIO3, &GPIO_InitStruct);

	//PT3
	GPIO_InitStruct.GPIO_Pin 		=	GPIO_Pin_6; //seg
	GPIO_InitStruct.GPIO_Mode 		=	GPIO_Mode_IN;
	GPIO_InitStruct.GPIO_PuPd 		=	GPIO_PuPd_UP;//选择开启上拉 GPIO_PuPd_NOPULL;
	GPIO_InitStruct.GPIO_PTSR	 	=	GPIO_PTSR_WEAK;
	GPIO_Init(GPIO3, &GPIO_InitStruct);

#else
    UART_UART1_IOMAP(UART1_IOMAP_SWCLK_SWDIO); //将串口1复用

#endif
}

void uart_to_flash_deinit(void)
{
    RCC_SetSYSCLKDefault(); // 1：选择过渡时钟作为系统时钟 (默认项)；24MHz / 2 == 12mhz
    UART_DeInit(UART1);
    UART_UART1_IOMAP(UART1_IOMAP_P36_P37); //将串口1复用
    UserTimer0_Init();
}

void uart_to_flash_get_buf(void) // 串口读取
{
    u8 Res;
    if (UART_GetFlagStatus(UART1, UART_Flag_RXNE)) // 接收中断(接收到的数据必须是0x0d 0x0a结尾)
    {
        Res = UART_ReceiveData(UART1);
    }
}

void uart_to_flash_pro(void)
{
    while (KEY_POWERIO == 0);
    delay_ms(50);
    while (KEY_POWERIO == 1)
    {
        TBEF_mainFun();
    }
}
